package com.ympc.controller;

import com.ympc.pojo.Student;
import com.ympc.pojo.Teacher;
import com.ympc.pojo.Users;
import com.ympc.service.StudentService;
import com.ympc.service.TeacherService;
import com.ympc.service.UsersService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
public class UsersController {
    @Resource
    private UsersService usersServiceImpl;
    
    @Resource
    private StudentService studentService;
    
    @Resource
    private TeacherService teacherService;
    @RequestMapping(value = "/")
    public String index(){

        return "login";
    }


    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(Users users, Model model, HttpSession session){
        // 支持使用用户名（学号）或姓名登录
        Users dbUser = null;
        if (users.getRole() != null && users.getRole().equals("student")) {
            // 学生：尝试使用用户名或姓名登录
            dbUser = usersServiceImpl.loginByUsernameOrName(users.getUsername(), "student");
        } else {
            // 教师和管理员：只使用用户名登录
            dbUser = usersServiceImpl.getUserByUsername(users.getUsername());
        }
        
        if (dbUser == null) {
            model.addAttribute("logints", "用户名或姓名不存在");
            return "login";
        }
        
        // 验证密码
        if (!dbUser.getPassword().equals(users.getPassword())) {
            model.addAttribute("logints", "密码错误");
            return "login";
        }
        
        // 验证角色
        if (!dbUser.getRole().equals(users.getRole())) {
            model.addAttribute("logints", "该用户无权限");
            return "login";
        }
        
        // 登录成功
        session.setAttribute("user", dbUser);
        
        // 如果是学生，获取学生信息并添加到session
        if (users.getRole().equals("student")) {
            try {
                Student student = studentService.getStudentByUserId(dbUser.getId());
                if (student != null) {
                    session.setAttribute("student", student);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "student";
        } else if (users.getRole().equals("teacher")) {
            // 如果是教师，获取教师信息并添加到session
            try {
                Teacher teacher = teacherService.getTeacherByUserId(dbUser.getId());
                if (teacher != null) {
                    session.setAttribute("teacher", teacher);
                } else {
                    // 如果还没有教师信息，创建一个空的教师对象
                    teacher = new Teacher();
                    teacher.setUserId(dbUser.getId());
                    session.setAttribute("teacher", teacher);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "teacher";
        } else {
            return "admin";
        }
    }


}
